// filepath: /home/debi/IdeaProjects/LearnRust/Work/basic/src/stdx/iox/read.rs
#[cfg(test)]
mod filex {
    use std::path::Path;
    use std::{env, fs};

    #[test]
    fn evn_vars() {
        let buf = env::current_dir().unwrap();
        println!("buf {:?}", buf);

        let path_file = buf.join(Path::new("src/iox/read.rs"));

        println!("path_file {:?}", path_file);
        let contents =
            fs::read_to_string(path_file).expect("Something went wrong reading the file");

        println!("{}", contents)
    }
}

// ========== 新增 read.rs：读取文件与路径拼接示例 ==========
use std::fs;
use std::path::Path;

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;

    #[test]
    fn read_file_to_string_example() {
        // 在临时目录创建一个文件并读取它
        let cwd = env::current_dir().unwrap();
        let dir = cwd.join("src/stdx/iox/temp_read");
        std::fs::create_dir_all(&dir).unwrap();
        let f = dir.join("example.txt");
        std::fs::write(&f, "hello read").unwrap();

        let s = fs::read_to_string(&f).unwrap();
        assert_eq!(s, "hello read");

        std::fs::remove_file(&f).unwrap();
        std::fs::remove_dir(&dir).unwrap();
    }

    #[test]
    fn path_join_and_exists_example() {
        let cwd = env::current_dir().unwrap();
        let p = cwd.join(Path::new("src/stdx/iox/read.rs"));
        assert!(p.exists());
    }
}
